{% extends 'system_template/public/base_main.html' %}
{% block cite %}内容发布{% endblock %}
{% block head %}
    <script charset="utf-8" src="{{ url_for('static',filename='lib/kindeditor/kindeditor-all.js') }}"></script>
    <script charset="utf-8" src="{{ url_for('static',filename='lib/kindeditor/lang/zh-CN.js') }}"></script>
    <script src="{{ url_for('static',filename='js/cxcolor.min.js') }}"></script>
    <script>
        KindEditor.ready(function (K) {
            window.editor0 = K.create('#content', {
                uploadJson: '{{ url_for('sys_bp.upload') }}',
                allowFileManager: false,
                wellFormatMode: true,
                resizeType: 1,
                formatUploadUrl: false
            });
            window.editor1 = K.create('#pay_content', {
                uploadJson: '{{ url_for('sys_bp.upload') }}',
                allowFileManager: false,
                wellFormatMode: true,
                resizeType: 1,
                formatUploadUrl: false
            });
        });
    </script>
{% endblock %}
{% block content %}
    <form class="layui-form">
        {#  非复选框   #}
        {% for foo in form %}
            {% if foo.type!='BooleanField' %}
                <div class="layui-form-item">
                    <label class="layui-form-label">{{ foo.label }}</label>
                    <div class="layui-input-block">
                        {{ foo }}
                        <div class="layui-form-mid">
                            {% if foo.name=='title' %}
                                内容标题[<span style="color: red">* 必填</span>]
                            {% elif foo.name=='thumb_img' %}
                                <a class="upload_img">
                                    <i class="layui-icon">&#xe67c;</i> 上传图片</a>
                                上传缩略图
                            {% elif foo.name=='keyword' %}
                                多个关键词用(,)逗号分隔
                            {% elif foo.name=='reward' %}
                                单位(¥)/保留两位小数
                            {% elif foo.name=='content' %}
                                付费模式下为免费可见部分
                            {% elif foo.name=='pay_content' %}
                                切换免费模式 将<span style="color: red"> 清空付费内容区域</span>
                            {% elif foo.name=='download' %}
                                免费模式不影响/付费模式[付费可见]
                            {% elif foo.name=='clicks' %}
                                点击数必须为<span style="color: red"> 整数</span>
                            {% elif foo.name=='update_time' %}
                                发布时间[<span style="color: red">* 必填</span>] 小于当前时间显示
                            {% endif %}
                        </div>
                    </div>
                </div>
            {% endif %}
        {% endfor %}
        {#  复选框   #}
        <div class="layui-form-item">
            <label class="layui-form-label">其他属性</label>
            <div class="layui-input-block">
                {% for foo in form %}
                    {% if foo.type=='BooleanField' %}
                        {{ foo }}
                    {% endif %}
                {% endfor %}
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <a class="layui-btn" id="submit">保存发布</a>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
        <input class="layui-upload-file" type="file" name="file" id="upload" accept="image/*">
    </form>
{% endblock %}
{% block footer %}
    <script>
        $(function () {
            // 初始化 隐藏
            reward = $("label[for='reward']").parent('label').parent('div');
            pay_content = $("label[for='pay_content']").parent('label').parent('div');
            $("#title_color").cxColor();
            {% if form.permissions.data %}
                reward.show();
                pay_content.show();
            {% else %}
                reward.hide();
                pay_content.hide();
            {% endif %}
            layui.use(['form', 'laydate'], function () {
                form.on('radio', function (data) {
                    var res = data.value;
                    if (res === "0") {
                        reward.hide();
                        pay_content.hide();
                        reward.find('input').val('');
                        editor1.html('')
                    } else {
                        reward.show();
                        pay_content.show();
                    }
                });
                $ = layui.jquery;
                form = layui.form;
                var laydate = layui.laydate;
                laydate.render({
                    type:'datetime',
                    elem: '#update_time' //指定元素
                });
            });
            $('.upload_img:eq(0)').click(function () {
                open_file();
                var input = $("#upload");
                input.change(function () {
                    if ($(this).val() !== "") {
                        upload_img('{{ url_for('sys_bp.upload_img') }}', '#thumb_img')
                    }
                    input.val('')
                })
            });
            $('#submit').click(function () {
                editor0.sync();
                editor1.sync();
                submit_post("{{ url_for('sys_bp.content_list') }}");
            });
        });
    </script>
{% endblock %}